﻿HmJS.$Import('core.element');

/*
---
description: fileoverview 文档支持 - 页面主体自适应页面

win.addEvent('domready', function () {
if (Browser.ie6) {
//IE6下pre标签如果不设置宽度值，无法自动给出滚动条
//通过JS计算宽度会导致浏览器假死，尚未找到合适的解决方案。
} else {
var indexDocWin = new clsDocWin();
}
});
...
*/

HmJS.register('util.dom.DocAutoResize', function ($ns) {

	return new Class({

		Implements: [Options],

		Binds: ['adapt', 'destroy'],

		options: {
			els: {
				box: 'container',
				head: 'header',
				menu: 'menu',
				content: 'content',
				foot: 'footer'
			},
			minWidth: 400, //内容区域最小宽度
			minHeight: 50	//内容区域最小高度
		},

		initialize: function (options) {
			this.setOptions(options);
			this.build(this.options);
		},

		build: function (ops) {
			this.els = {};
			Object.each(ops.els, function (val, key) {
				this.els[key] = $(val);
			} .bind(this));

			this.adapt();
			this.setEvent('add');
		},

		setEvent: function (motion) {
			var m = motion + 'Event';
			window[m]('resize', this.adapt);
			window[m]('beforeunload', this.destroy);
		},

		adapt: function () {
			var ops = this.options,
					wsize = window.getSize(),
					hsize = this.els.head.getSize(),
					fsize = this.els.foot.getSize(),
					msize = this.els.menu.getSize(),
					areaWidth = Math.max(wsize.x - msize.x - 3, ops.minWidth),
					areaHeight = Math.max(wsize.y - hsize.y - fsize.y, ops.minHeight);

			this.els.box.set('styles', { width: Math.max(wsize.x, areaWidth + msize.x + 3) });
			this.els.menu.set('styles', { height: areaHeight });
			this.els.content.set('styles', { height: areaHeight });
			this.els.content.set('styles', { width: areaWidth });
		},

		destroy: function () {
			this.setEvent('remove');
		}

	});

});